<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef"
             label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="image" :rules="{ required: '必传'}">
        <el-upload
          :action="dataForm.upload"
          :show-file-list="false"
          :before-upload="beforeUploadHandle"
          :on-success="successHandle">
          <el-image v-if="dataForm.image" :src="dataForm.image" class="avatar" style="max-width: 200px"></el-image>
          <el-icon v-else :size="30">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">维修</el-radio>
          <el-radio :label="1">可用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="时间互斥" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <wang-eidtor v-model="dataForm.content" placeholder="请输入内容"></wang-eidtor>
      </el-form-item>
      <!--<el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">
          <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
        </el-form-item>-->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {getToken} from "@/utils/cache";
import {IObject} from "@/types/interface";
import WangEidtor from "@/components/wang-editor/index.vue";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '', name: '', image: '', content: '', status: 0, type: 1,
  upload: ""
});

const rules = ref({
  name: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  // image: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  content: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  status: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  type: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.upload = `${window.SITE_CONFIG['apiURLUpload']}?token=${getToken()}&type=machine`

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/machine/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 上传之前
const beforeUploadHandle = (file: IObject) => {
  if (file.type !== "image/jpg" && file.type !== "image/jpeg" && file.type !== "image/png" && file.type !== "image/gif") {
    ElMessage.error("只支持jpg、png、gif格式文件！");
    return false;
  }
};

// 上传成功
const successHandle = (res: IObject, file: IObject, list: IObject[]) => {
  dataForm.image = res.data.url;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/machine", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
